<?php
class NewsAction extends Action{
	public function showNewsColumn(){
		$data = M("News_type")->where("is_display = 1")->select();
		if ($data == false && !is_array($data)){
			$res = ["code"=>"wrong"];	
		}
		$res = ["code"=>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($res));
	}
	
	public function showNewsBanner(){
		$data = M("News")->where("nid=0")->find();
		if ($data === false && !is_array($data)){
			$res = ["code"=>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		
		//解析图片
		$image = D('attach')->where('attach_id='.$data["pic"])->find();
		$image_url = $image['save_path'].$image['save_name'];
		$data["pic"] = getImageUrl($image_url);

		$res = ["code"=>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($res));
	}
	public function showNews(){
		$tid = t($_REQUEST["tid"]);
		if (!isset($tid)){
			$res = ["code"=>"wrong","massage"=>"参数不能为空！"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		//分页设置
		$pagecount=isset($_POST['"pagecount'])?$_POST['pagecount']:"10";//每页显示数量
		$pagenum=$pagecount*((isset($_POST['pagenum'])?$_POST['pagenum']:"1")-1);//当前页数
		
		$data = M("News")->where("is_display = 1 and tid = $tid")->limit($pagenum,$pagecount)->select();
		if ($data === false && !is_array($data)){
			$res = ["code"=>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		for ($i=0;$i<count($data);$i++){
			//解析图片
			$image = D('attach')->where('attach_id='.$data[$i]["pic"])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$data[$i]["pic"] = getImageUrl($image_url);
			
		}
		if(count($data)<1){
			$res = ["code"=>"success","data"=>[]];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		$res = ["code"=>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($res));
	}
}